from sqlalchemy.orm import Session

from core.entity.do.models import CmsGaodeService
from core.entity.vo.view_models import CmsGaodeServiceVO


class CmsGaodeServiceDao:
    """
    高德终端dao
    """

    @classmethod
    def get_cms_gaode_service_by_id(cls, db: Session, id: int):
        """
        根据id查询高德终端信息
        :param db: orm对象
        :param id: 主键ID
        :return: 高德终端信息对象
        """
        cms_gaode_service = db.query(CmsGaodeService) \
            .filter(CmsGaodeService.id == id) \
            .first()

        return cms_gaode_service

    @classmethod
    def get_cms_gaode_service_by_service_id(cls, db: Session, service_id: str):
        """
        根据终端id查询高德终端信息
        :param db: orm对象
        :param service_id: 终端id
        :return: 高德终端信息对象
        """
        cms_gaode_service = db.query(CmsGaodeService) \
            .filter(CmsGaodeService.service_id == service_id) \
            .first()

        return cms_gaode_service

    @classmethod
    def get_cms_gaode_service_like_name(cls, db: Session,service_name: str):
        """
        获取所有的参数配置信息
        :param db: orm对象
        :param service_name: 终端名称
        :return: 高德终端信息对象集合
        """
        cms_gaode_service_list = db.query(CmsGaodeService) \
            .filter(CmsGaodeService.service_name.like(f'%{service_name}%') if service_name else True) \
            .all()

        return cms_gaode_service_list

    @classmethod
    def add_cms_gaode_service_dao(cls, db: Session, cms_gaode_service: CmsGaodeServiceVO):
        """
        新增高德终端
        :param db: orm对象
        :param cms_gaode_service: 高德终端对象
        :return:
        """
        model_dict = CmsGaodeService(**cms_gaode_service.dict())
        db.add(model_dict)
        db.flush()

        return model_dict

    @classmethod
    def edit_cms_gaode_service_dao(cls, db: Session, cms_gaode_service: dict):
        """
        编辑参数配置数据库操作
        :param db: orm对象
        :param cms_gaode_service: 需要更新的参数配置字典
        :return:
        """
        db.query(CmsGaodeService) \
            .filter(CmsGaodeService.id == cms_gaode_service.get('id')) \
            .update(cms_gaode_service)

    @classmethod
    def delete_cms_gaode_service_dao(cls, db: Session, id: str):
        """
        编辑参数配置数据库操作
        :param db: orm对象
        :param id: 服务编码
        :return:
        """
        db.query(CmsGaodeService) \
            .filter(CmsGaodeService.id == id) \
            .delete()
